//
//  OpenMyGroup.m
//  timelyRain
//
//  Created by Jiafei Wei on 16/9/22.
//  Copyright © 2016年 Jiafei Wei. All rights reserved.
//

#import "OpenMyGroup.h"
#import "TContactGroup.h"
#import "ContactCell.h"
#import "TContact.h"
#import "TContactHeader.h"
#import "DbManage.h"
@interface OpenMyGroup ()
<TContactHeaderDelegate,UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *table;
@property (nonatomic,strong)NSMutableArray *grounp;
@end

@implementation OpenMyGroup

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets=NO;//不知道这个
    [super setTitleView:self.view withName:@"我的群组"];
    _table.delegate =self;
    _table.dataSource =self;
    _grounp =[[DbManage shareDbManage]queryConatctGroupArray];
    //[_table reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
#pragma mark - 数据源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _grounp.count;
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    TContactGroup *group = _grounp[section];
    return group.isExpend?group.child.count : 0;//根据是否是展开的状态决定组的数量
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ContactCell *cell =  [ContactCell setContactCellWithTableView:tableView];
    TContactGroup *group = _grounp[indexPath.section];
    TContact *info = group.child[indexPath.row];
    cell.data =info;
    return cell;
}
#pragma mark - 代理的方法//
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    TContactHeader *head =[TContactHeader headerViewWithTableView:tableView];
    head.delegate=self;
    TContactGroup *group = _grounp[section];
    head.data = group;//在这里显示头部的信息
    head.tag = section;
    return head;
}

#pragma mark - headerView的代理方法
- (void)headerViewDidClickedNameView:(TContactHeader *)headerView//分组的搞定了啊
{
    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:headerView.tag];//这个效果最好
    [_table reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];//刷新listview
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1f;
}
//一定要重写该方法
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //单选
}
@end
